﻿#include "FITKSEAAbstractJunction.h"
#include "FITKAcousticsDiscModel.h"
#include "FITKAcousticsSEAModel.h"
#include "FITKAbstractSEASubSys.h"

#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"

#include <QDebug>

namespace Acoustics
{
    FITKSEAAbstractJunction::~FITKSEAAbstractJunction()
    {
        //纯虚函数
    }

    void FITKSEAAbstractJunction::appendUserDef(FITKSEAJunUserDefStr & userDef)
    {
        _userDefs.append(userDef);
    }

    void FITKSEAAbstractJunction::replaceUserDefs(QList<FITKSEAJunUserDefStr>& userDefs)
    {
        _userDefs = userDefs;
    }

    QList<FITKSEAJunUserDefStr> FITKSEAAbstractJunction::getUserDefs()
    {
        return _userDefs;
    }

    QString FITKSEAAbstractJunction::getMatrixForm()
    {
        return _matrixForm;
    }

    void FITKSEAAbstractJunction::setMatrixForm(QString type)
    {
        _matrixForm = type;
    }

    void FITKSEAAbstractJunction::initUserDefineMatrix(QList<int>& subSysIdList)
    {
        //获取离散数据
        FITKAcousticsDiscModel* discModel = FITKAPP->getGlobalData()->getMeshData<FITKAcousticsDiscModel>();
        if (discModel == nullptr)return;
        FITKAcousticsSEAModel* SEAModel = discModel->getSEAModel();
        if (SEAModel == nullptr)return;
        FITKAcousticsSEASubSysManager* subManager = SEAModel->getSEASubSysManager();
        if (subManager == nullptr)return;
        //清空数据
        _userDefs.clear();

        //判断是否在对称轴,并且指定初值
        auto isSymmetry = [&](FITKSEAJunUserDefStr& str)->bool {
            if (str._columnIndex > str._rowIndex) {
                str._spectrumID = -2;
                return false;
            }
            else if (str._columnIndex == str._rowIndex) {
                return true;
            }
            else {
                str._spectrumID = -1;
                return false;
            }
        };

        //创建user-define结构体
        auto creatUserDefinStr = [&](const int& rowSubIndex, const int& rowSubID, const int& rowIndex) {
            int columIndex = 1;
            for (int columSubID : subSysIdList) {
                FITKAbstractSEASubSys* subSysColum = subManager->getDataByID(columSubID);
                if (subSysColum == nullptr)continue;
                switch (subSysColum->getSEASubSysType()) {
                case Acoustics::FITKAbstractSEASubSys::SEABeam: {
                    FITKSEAJunUserDefStr str;
                    str._subsysRowID = rowSubID;
                    str._subsysColumnID = columSubID;
                    str._subsysRowIndex = rowSubIndex;
                    str._rowIndex = rowIndex;
                    //X
                    str._subsysColumnIndex = 0;
                    str._columnIndex = columIndex;
                    columIndex++;
                    if (!isSymmetry(str)) {
                        _userDefs.append(str);
                    }
                    //Y
                    str._subsysColumnIndex = 1;
                    str._columnIndex = columIndex;
                    columIndex++;
                    if (!isSymmetry(str)) {
                        _userDefs.append(str);
                    }
                    //E
                    str._subsysColumnIndex = 2;
                    str._columnIndex = columIndex;
                    columIndex++;
                    if (!isSymmetry(str)) {
                        _userDefs.append(str);
                    }
                    //T
                    str._subsysColumnIndex = 3;
                    str._columnIndex = columIndex;
                    columIndex++;
                    if (!isSymmetry(str)) {
                        _userDefs.append(str);
                    }
                    break;
                }
                case Acoustics::FITKAbstractSEASubSys::SEAFlatPlate:
                case Acoustics::FITKAbstractSEASubSys::SEASinglyCurvedShell:
                case Acoustics::FITKAbstractSEASubSys::SEADoublyCurvedShell:
                case Acoustics::FITKAbstractSEASubSys::SEACylinderShell: {
                    FITKSEAJunUserDefStr str;
                    str._subsysRowID = rowSubID;
                    str._subsysColumnID = columSubID;
                    str._subsysRowIndex = rowSubIndex;
                    str._rowIndex = rowIndex;
                    //E
                    str._subsysColumnIndex = 0;
                    str._columnIndex = columIndex;
                    columIndex++;
                    if (!isSymmetry(str)) {
                        _userDefs.append(str);
                    }
                    //S
                    str._subsysColumnIndex = 1;
                    str._columnIndex = columIndex;
                    columIndex++;
                    if (!isSymmetry(str)) {
                        _userDefs.append(str);
                    }
                    //F
                    str._subsysColumnIndex = 2;
                    str._columnIndex = columIndex;
                    columIndex++;
                    if (!isSymmetry(str)) {
                        _userDefs.append(str);
                    }
                    break;
                }
                case Acoustics::FITKAbstractSEASubSys::SEACavity: {
                    FITKSEAJunUserDefStr str;
                    str._subsysRowID = rowSubID;
                    str._subsysColumnID = columSubID;
                    str._subsysRowIndex = rowSubIndex;
                    str._rowIndex = rowIndex;
                    str._subsysColumnIndex = 0;
                    str._columnIndex = columIndex;
                    columIndex++;
                    if (!isSymmetry(str)) {
                        _userDefs.append(str);
                    }
                    break;
                }
                }
            }
        };

        //初始化矩阵数据
        int rowIndex = 1;
        for (int rowSubID : subSysIdList) {
            FITKAbstractSEASubSys* rowSubSys = subManager->getDataByID(rowSubID);
            if (rowSubSys == nullptr)continue;
            switch (rowSubSys->getSEASubSysType()) {
            case Acoustics::FITKAbstractSEASubSys::SEABeam: {
                //X
                creatUserDefinStr(0, rowSubID, rowIndex);
                rowIndex++;
                //Y
                creatUserDefinStr(1, rowSubID, rowIndex);
                rowIndex++;
                //E
                creatUserDefinStr(2, rowSubID, rowIndex);
                rowIndex++;
                //T
                creatUserDefinStr(3, rowSubID, rowIndex);
                rowIndex++;
                break;
            }
            case Acoustics::FITKAbstractSEASubSys::SEAFlatPlate:
            case Acoustics::FITKAbstractSEASubSys::SEASinglyCurvedShell:
            case Acoustics::FITKAbstractSEASubSys::SEADoublyCurvedShell:
            case Acoustics::FITKAbstractSEASubSys::SEACylinderShell: {
                //E
                creatUserDefinStr(0, rowSubID, rowIndex);
                rowIndex++;
                //S
                creatUserDefinStr(1, rowSubID, rowIndex);
                rowIndex++;
                //F
                creatUserDefinStr(2, rowSubID, rowIndex);
                rowIndex++;
                break;
            }
            case Acoustics::FITKAbstractSEASubSys::SEACavity: {
                creatUserDefinStr(0, rowSubID, rowIndex);
                rowIndex++;
                break;
            }
            }
        }
    }
}
